#include "stdafx.h"
#include "XLogoButtonImpl.h"

#include <Util.h>
#include <UIUtil.h>
#include <DrawUtil.h>

//////////////////////////////////////////////////////////////////////////

static const int g_LogoTextInternal = 12;
static const int g_LogoLeftMargin = 12;
static const int g_TextRightMargin = 10;

//////////////////////////////////////////////////////////////////////////

template <class I>
CXLogoButtonImpl<I>::CXLogoButtonImpl()
{
    m_pLogoDraw = NULL;
    m_sizeLogo.cx = 0;
    m_sizeLogo.cy = 0;
}

template <class I>
CXLogoButtonImpl<I>::~CXLogoButtonImpl()
{
}

//////////////////////////////////////////////////////////////////////////
// IXLogoButton
template <class I>
void CXLogoButtonImpl<I>::put_LogoDraw(IN IXImageDraw* pLogoDraw)
{
    XSAFE_RELEASE(m_pLogoDraw);
    m_pLogoDraw = pLogoDraw;

    IXTextEnv* pTextEnv = NULL;
    get_TextEnv(&pTextEnv);
    if(m_pLogoDraw == NULL)
    {
        pTextEnv->put_DrawFlags(pTextEnv->get_DrawFlags() | DT_CENTER);
    }
    else
    {
        m_pLogoDraw->get_ImageSize(&m_sizeLogo);
        m_pLogoDraw->AddRef();
        pTextEnv->put_DrawFlags(pTextEnv->get_DrawFlags() & (~DT_CENTER));
    }

    AutoSizeImpl(FALSE);
}

template <class I>
void CXLogoButtonImpl<I>::put_LogoSize(IN SIZE* sizeLogo)
{
    m_sizeLogo = *sizeLogo;
}

//////////////////////////////////////////////////////////////////////////
// IXFrame
template <class I>
void CXLogoButtonImpl<I>::GetAutoSize(OUT LPSIZE pAutoSize)
{
    SIZE sizeImage = *pAutoSize;
    if(m_pButtonDraw == NULL)
    {
        superclass::GetAutoSize(&sizeImage);
    }
    else
    {
        m_pButtonDraw->get_ImageSize(&sizeImage);
    }

    SIZE sizeText = {0};
    if(!m_strText.empty())
    {
        CXFrameImpl<I>::GetAutoSize(&sizeText);
        sizeText.cx += 4;
        sizeText.cy += 4;
    }

    if(m_pLogoDraw != NULL)
    {
        sizeText.cx += g_LogoLeftMargin + m_sizeLogo.cx + g_LogoTextInternal + g_TextRightMargin;
        sizeText.cy = max(sizeText.cy, m_sizeLogo.cy);
    }

    pAutoSize->cx = max(sizeImage.cx, sizeText.cx);
    pAutoSize->cy = max(sizeImage.cy, sizeText.cy);
}

template <class I>
void CXLogoButtonImpl<I>::Draw(XCANVAS xCanvas, const RECT& rcDraw)
{
    CXFrameImpl<I>::Draw(xCanvas, rcDraw);

    RECT rcWnd;
    get_WindowRect(&rcWnd);

    HRESULT hResult = S_OK;

    if(m_pButtonDraw != NULL)
    {
        hResult = m_pButtonDraw->Draw(xCanvas, &rcWnd);
    }

    if(m_pLogoDraw != NULL)
    {
        RECT rcLogo;
        rcLogo.left = rcWnd.left + g_LogoLeftMargin;
        rcLogo.top = rcWnd.top + (rcWnd.bottom - rcWnd.top - m_sizeLogo.cy) / 2;
        rcLogo.right = rcLogo.left + m_sizeLogo.cx;
        rcLogo.bottom = rcLogo.top + m_sizeLogo.cy;
        m_pLogoDraw->Draw(xCanvas, &rcLogo);

        rcWnd.left += g_LogoLeftMargin + m_sizeLogo.cx + g_LogoTextInternal;
    }

    if(!m_strText.empty())
    {
        IXFont* pFont = NULL;
        IXTextEnv* pTextEnv = NULL;

        get_Font(&pFont);
        get_TextEnv(&pTextEnv);

        DrawUtil::XDrawText(xCanvas, m_strText, m_strText.Length(), &rcWnd, pTextEnv, pFont);
    }
}

//////////////////////////////////////////////////////////////////////////
// IXBaseObj
template <class I>
void CXLogoButtonImpl<I>::OnFinalRelease()
{
    superclass::OnFinalRelease();

    XSAFE_RELEASE(m_pLogoDraw);
}
